<template>
  <div class="section">
    <div class="add-box">
      <router-link tag="span" to="/add-product">
        <el-button type="primary" size="small">
          新建产品
        </el-button>
      </router-link>
    </div>
    <div class="search-box">
      <el-input
        class="input-product-name"
        v-model="productName"
        placeholder="请输入商品名称"
      ></el-input>
      <el-button type="primary" @click="search">搜索</el-button>
    </div>
    <el-table border class="table-box" :data="tableData">
      <el-table-column prop="name" label="商品名称"></el-table-column>
      <el-table-column prop="price" label="商品价格"></el-table-column>
      <el-table-column prop="desc" label="描述"></el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="del(scope.row.id)"
            >删除</el-button
          >
          <el-button type="text" size="small">查看</el-button>
          <el-button type="text" size="small">
            <router-link tag="span" :to="'/add-product?id=' + scope.row.id"
              >编辑</router-link
            >
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="sizes,prev, pager, next"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<style lang="less">
.section {
  .add-box {
    margin-bottom: 15px;
  }
  .search-box {
    .input-product-name {
      width: 180px;
      margin-right: 10px;
      margin-bottom: 15px;
    }
  }
  .table-box {
    margin-bottom: 10px;
  }
}
</style>

<script>
import apiUrl from '../utils/apis'
export default {
  data() {
    return {
      tableData: null,
      total: 10,
      currentPage: 1,
      pageSize: 10,
      productName: '',
    }
  },
  methods: {
    getProductList() {
      var param = {
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        name: this.productName,
      }
      this.$axios
        .get(apiUrl.queryProduct + '?' + this.$qs.stringify(param))
        .then((res) => {
          this.total = res.data.total
          this.tableData = res.data.results
        })
        .catch((err) => {
          console.log(err)
        })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getProductList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getProductList()
    },
    del(id) {
      let that = this
      that
        .$confirm('是否要删除当前产品？', '确认信息', {
          distinguishCancelAndClose: true,
          confirmButtonText: '确认',
          cancelButtonText: '取消',
        })
        .then(() => {
          console.log(id)
          that.$axios
            .delete(apiUrl.deleteProduct + id)
            .then((res) => {
              that.getProductList()
              console.log(res)
            })
            .catch((err) => {
              console.log(err)
            })
        })
    },
    search() {
      this.getProductList()
    },
  },
  mounted() {
    this.getProductList()
  },
}
</script>
